package com.sgb.jicai.weight;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.ItemPriceRolePopWindowListBinding;
import com.sgb.jicai.entity.OfferGroupBuyingDetailsEntity;
import com.sgb.link.utils.JiCaiEditInputFilter;
import com.swgk.core.BR;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;
import java.util.List;

/**
 * 参与报价、价格规则设备弹窗列表适配器
 */
public class PriceRoleListAdapter extends BaseRecycleAdapter<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> {

    private Context context;
    private List<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> dataList;
    private DelOneDataCallBack delOneDataCallBack;

    public PriceRoleListAdapter(Context context, List<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> datas) {
        super(context, R.layout.item_price_role_pop_window_list, datas);
        this.context = context;
        dataList = datas;
    }

    @Override
    public void convert(ItemViewHolder holder, OfferGroupBuyingDetailsEntity.GroupBuyingDetails priceRule) {
        holder.setBinding(BR.viewModel, priceRule);
        ItemPriceRolePopWindowListBinding binding = (ItemPriceRolePopWindowListBinding) holder.getmBinding();
        binding.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != getDatas() && getDatas().size() > 0) {
                    if (getDatas().get(0) == priceRule) {
                        MToast.showToast(context, "第一位不可删除");
                    } else {
                        if (delOneDataCallBack != null) {
                            delOneDataCallBack.delOne(priceRule);
                        }
                    }
                }
            }
        });

        setOnFocusChange(binding.etStart, priceRule, 1);
        setOnFocusChange(binding.etEnd, priceRule, 2);
        setOnFocusChange(binding.etPrice, priceRule, 3);

        if (null != getDatas() && getDatas().size() > 0) {
            if (getDatas().size() == 1) {
                binding.ivDel.setVisibility(View.INVISIBLE);
                setCanEdit(binding.etStart);
                setCanEdit(binding.etEnd);
                setCanEdit(binding.etPrice);
            } else if (getDatas().size() > 1) {
                if (getDatas().get(getDatas().size() - 1) == priceRule) {
                    binding.ivDel.setVisibility(View.VISIBLE);
                    setConNotEdit(binding.etStart);
                    setCanEdit(binding.etEnd);
                    setCanEdit(binding.etPrice);
                } else {
                    binding.ivDel.setVisibility(View.INVISIBLE);
                    setConNotEdit(binding.etStart);
                    setConNotEdit(binding.etEnd);
                    setConNotEdit(binding.etPrice);
                }
            }
        }
        //小数点后保留两位数字
        binding.etStart.setFilters(new JiCaiEditInputFilter[]{new JiCaiEditInputFilter(999999999999.99, 2)});
        binding.etEnd.setFilters(new JiCaiEditInputFilter[]{new JiCaiEditInputFilter(999999999999.99, 2)});
        binding.etPrice.setFilters(new JiCaiEditInputFilter[]{new JiCaiEditInputFilter(999999999999.99, 2)});
    }

    private void setCanEdit(EditText editText) {
        editText.setFocusableInTouchMode(true);
        editText.setFocusable(true);
//        editText.requestFocus();
    }

    private void setConNotEdit(EditText editText) {
        editText.setFocusable(false);
        editText.setFocusableInTouchMode(false);
    }

    //EditText焦点发生变化
    private void setOnFocusChange(EditText editText, OfferGroupBuyingDetailsEntity.GroupBuyingDetails priceRule, int type) {
        editText.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                } else {
                    // 此处为失去焦点时的处理内容
                    String editValue = editText.getText().toString();
                    if (!TextUtils.isEmpty(editValue)) {
                        switch (type) {
                            case 1:
                                if (!TextUtils.isEmpty(priceRule.getIntervalEndValue()) && Double.valueOf(editValue) > Double.valueOf(priceRule.getIntervalEndValue())) {
                                    MToast.showToast(context, "加盟量中最小值不得大于最大值");
                                    editText.setText("");
                                } else {
                                    priceRule.setIntervalStartValue(editValue);
                                }
                                break;
                            case 2:
                                if (!TextUtils.isEmpty(priceRule.getIntervalStartValue()) && Double.valueOf(editValue) < Double.valueOf(priceRule.getIntervalStartValue())) {
                                    MToast.showToast(context, "加盟量中最大值不得小于最小值");
                                    editText.setText("");
                                } else {
                                    priceRule.setIntervalEndValue(editValue);
                                }
                                break;
                            case 3:
                                priceRule.setPrice(editValue);
                                break;
                        }
                    }
                }
            }
        });
    }

    private void myTextWatcher(EditText editText, OfferGroupBuyingDetailsEntity.GroupBuyingDetails priceRule, int type) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    return;
                }
                switch (type) {
                    case 1:
                        if (!TextUtils.isEmpty(priceRule.getIntervalEndValue()) && Double.valueOf(s.toString().trim()) > Double.valueOf(priceRule.getIntervalEndValue())) {
                            MToast.showToast(context, "加盟量中最小值不得大于最大值");
                            editText.setText("");
                        } else {
                            priceRule.setIntervalStartValue(s.toString().trim());
                        }
                        break;
                    case 2:
                        if (!TextUtils.isEmpty(priceRule.getIntervalStartValue()) && Double.valueOf(s.toString().trim()) < Double.valueOf(priceRule.getIntervalStartValue())) {
                            MToast.showToast(context, "加盟量中最大值不得小于最小值");
                        } else {
                            priceRule.setIntervalEndValue(s.toString().trim());
                        }
                        break;
                    case 3:
                        priceRule.setPrice(s.toString().trim());
                        break;
                }
            }
        });
    }


    public void setDelOneDataCallBack(DelOneDataCallBack delOneDataCallBack) {
        this.delOneDataCallBack = delOneDataCallBack;
    }

    public interface DelOneDataCallBack {
        void delOne(OfferGroupBuyingDetailsEntity.GroupBuyingDetails priceRule);
    }

}
